package com.giCompiler.dao;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.giCompiler.bean.Projeto;
import com.giCompiler.util.HibernateUtil;

public class ProjetoDao {
	 
	public void inserir(Projeto projeto){ 
	       
		Session session = HibernateUtil.getSessionFactory().openSession(); 		       
		Transaction transaction = null; 		       
		       
		try { 
		            
			transaction = session.beginTransaction(); 		           
		    session.save(projeto); 		           
			transaction.commit(); 
		        
		} catch (HibernateException e) { 
		           
			transaction.rollback(); 			           
			e.printStackTrace(); 
		      
		} finally { 		            
			session.close(); 	        
		} 

	} 
	
	@SuppressWarnings("unchecked")
	public List<Projeto> listar(int idUsuario){ 
	        
		Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		List<Projeto> projetos = new ArrayList <Projeto> ();
		        
		try { 
		            
			transaction = session.beginTransaction(); 			           
							 			 
			Query query = session.createSQLQuery("SELECT * FROM gicompiler.TB_PROJETO a, gicompiler.TB_VC_USU_PROJ b WHERE a.ID_PROJETO = b.ID_PROJETO AND b.ID_USUARIO = :idUsuario").addEntity(Projeto.class);						 			 
			query.setInteger("idUsuario", idUsuario);
			projetos = query.list();

			transaction.commit(); 
		       
		}catch (HibernateException e) { 
		        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		} finally { 		           
			session.close(); 		       
		} 
		
		return projetos;
	   
	} 	 
	 
	public void alterar(Projeto projeto){ 
	      
		Session session = HibernateUtil.getSessionFactory().openSession(); 		       
		Transaction transaction = null; 
		     
		try { 
		          
			transaction = session.beginTransaction(); 			            
			session.update(projeto);	           
			transaction.commit(); 
		      
		} catch (HibernateException e) { 
		            
			transaction.rollback(); 			            
			e.printStackTrace(); 
		       
		} finally { 
		           
			session.close(); 
	      
		} 	    
	} 	 
   
	public void excluir(Projeto projeto){ 
	        
		Session session = HibernateUtil.getSessionFactory().openSession(); 		       
		Transaction transaction = null; 
		        
		try { 
		           
			transaction = session.beginTransaction();
			session.delete(projeto); 		 
			transaction.commit(); 
		        
		} catch (HibernateException e) { 
		           
			transaction.rollback(); 		            
			e.printStackTrace(); 
		        
		} finally { 
		            
			session.close(); 
	       
		} 
	}	
	
	@SuppressWarnings("unchecked")
	public List<Projeto> pesquisar(String dados, String filtro, int tipo){
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		List<Projeto> projetos = new ArrayList <Projeto> ();    

		try{
		
	    transaction = session.beginTransaction(); 
	    
	    if(tipo == 1)
	    	projetos = session.createCriteria(Projeto.class).add(Restrictions.like(filtro, "%"+dados+"%")).list();	    
	    else if(tipo == 2)
	    	projetos = session.createCriteria(Projeto.class).add(Restrictions.like(filtro, Date.valueOf(dados))).list();
	    else if(tipo == 3)
	    	projetos = session.createCriteria(Projeto.class).add(Restrictions.like(filtro, dados)).list();	 
	    else
	    	projetos = session.createCriteria(Projeto.class).add(Restrictions.like(filtro, Integer.parseInt(dados))).list();
	    	
	    transaction.commit(); 
		
		} catch (HibernateException e) { 
	        
			transaction.rollback(); 		            
			e.printStackTrace(); 
		        
		} finally { 
		            
			session.close(); 
	       
		} 
		
        return projetos;
		
	}	
	
	@SuppressWarnings("unchecked")
	public List<Projeto> procurarNomeProjeto(String nome){
		
    	Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		List<Projeto> projetos = new ArrayList <Projeto> ();    

		try{
		
	    transaction = session.beginTransaction(); 
	    projetos = session.createCriteria(Projeto.class).add(Restrictions.eq("nome", nome)).list();	    
		transaction.commit(); 
		
		} catch (HibernateException e) { 
	        
			transaction.rollback(); 		            
			e.printStackTrace(); 
		        
		} finally { 
		            
			session.close(); 
	       
		} 
		
        return projetos;
        
	}
	
	@SuppressWarnings("unchecked")
	public List<Projeto> pegarProjeto(int idProjeto){
		
    	Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		List<Projeto> projetos = new ArrayList <Projeto> ();    
	
		try{
		    transaction = session.beginTransaction(); 
		    projetos = session.createCriteria(Projeto.class).add(Restrictions.eq("idProjeto", idProjeto)).list();	    
			transaction.commit(); 
		
		
		} catch (HibernateException e) { 
	        
			transaction.rollback(); 		            
			e.printStackTrace(); 
		        
		} finally { 
		            
			session.close(); 
	       
		} 
		
        return projetos;
        
	}

}